import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loading_indicator/loading_indicator.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:gold/app/user_provider.dart';
import 'package:gold/route/routes.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/cache_data.dart';
import 'package:gold/utils/size_extension.dart';
import 'package:gold/utils/xfg_color.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await CacheData().initInfo();
  /// webView设置
  if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
    await InAppWebViewController.setWebContentsDebuggingEnabled(kDebugMode);
  }
  /// 设置状态栏
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarColor: XfgColor.transparent,
    systemNavigationBarColor: XfgColor.transparent,
    statusBarBrightness: Brightness.light,
    statusBarIconBrightness: Brightness.light,
    systemNavigationBarIconBrightness: Brightness.light
  ));
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider<UserProvider>(create: (_) => UserProvider()),
    ],
    child: const MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) => RefreshConfiguration(
        headerBuilder: () => ClassicHeader(
          height: 56.xr,
          textStyle: const TextStyle(color: XfgColor.text1),
          releaseIcon: null,
          completeIcon: null,
          failedIcon: null,
          idleIcon: null,
          idleText: '下拉刷新',
          failedText: '刷新失败',
          releaseText: '松开刷新',
          completeText: '刷新成功',
          refreshingText: '',
          refreshingIcon: SizedBox(height: 32.xr, child: const LoadingIndicator(colors: [XfgColor.text2], indicatorType: Indicator.ballPulse)),
        ),
        footerBuilder:  () => CustomFooter(
          builder: (context, mode) {
            Widget? body;
            if (mode == LoadStatus.idle) {
              body = const Text("上拉加载更多", style: TextStyle(color: XfgColor.text1), key: ValueKey(1));
            } else if (mode == LoadStatus.loading) {
              body = SizedBox(height: 32.xr, child: const LoadingIndicator(colors: [XfgColor.text2], indicatorType: Indicator.ballPulse));
            } else if (mode == LoadStatus.canLoading) {
              body = const Text("松开加载", style: TextStyle(color: XfgColor.text1), key: ValueKey(2));
            } else if (mode == LoadStatus.failed) {
              body = const Text("加载失败", style: TextStyle(color: XfgColor.text1));
            } else if (mode == LoadStatus.noMore) {
              body = const Text("没有更多数据", style: TextStyle(color: XfgColor.text1));
            }
            return Container(alignment: Alignment.center, height: 56.xr, child: body);
          },
        ), // 配置默认底部指示器
        headerTriggerDistance: 56.w, // 头部触发刷新的越界距离
        springDescription: const SpringDescription(stiffness: 170, damping: 16, mass: 1.9), // 自定义回弹动画,三个属性值意义请查询flutter api
        maxOverScrollExtent: 100, //头部最大可以拖动的范围,如果发生冲出视图范围区域,请设置这个属性
        maxUnderScrollExtent: 100, // 底部最大可以拖动的范围
        enableScrollWhenRefreshCompleted: true, //这个属性不兼容PageView和TabBarView,如果你特别需要TabBarView左右滑动,你需要把它设置为true
        enableLoadingWhenFailed : true, //在加载失败的状态下,用户仍然可以通过手势上拉来触发加载更多
        hideFooterWhenNotFull: false, // Viewport不满一屏时,禁用上拉加载更多功能
        enableBallisticLoad: true, // 可以通过惯性滑动触发加载更多
        child: MaterialApp(
          title: '背单词',
          navigatorKey: XfgNav.navigatorKey,
          onGenerateRoute: onWsGenerateRoute,
          initialRoute: Routes.root,
          theme: ThemeData(
            primarySwatch: XfgColor.primarySwatch,
            primaryColor: XfgColor.theme,
            scaffoldBackgroundColor: XfgColor.background,
            visualDensity: VisualDensity.adaptivePlatformDensity,
            splashFactory: InkRipple.splashFactory,
          ),
          builder: EasyLoading.init(),
          locale: Locale('CH'),
          localizationsDelegates: const [
            RefreshLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
        ),
      ),
    );
  }
}
